package com.dmgis.qxfw.controller;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.dmgis.qxfw.common.DbConfig;
import com.dmgis.qxfw.service.ProcessRequestServiceI;

/**
 * 
 * @author lc
 * @ClassName: ProcessRequestController
 * @date 2018年9月10日 下午2:02:16
 * @Description:
 */
@Controller
public class ProcessRequestController {

	@Autowired
	private ProcessRequestServiceI nySereviceImpl;

	@RequestMapping("/HandleQxData")
	@ResponseBody
	public String processRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("-------------------------------------------------------------------------");
		String tableName = request.getParameter("method");
		String sqlType = request.getParameter("sqlType");
		String result = "";
		// Class<?> clazz = null;
		// String name = "";
		String sql = "";
		// 当前请求的url
		String uri = request.getRequestURI();
		// 根据url选择数据库
		String DBNAME = DbConfig.DBNAME;

		// response.setHeader("content-type", "application/json;charset=utf-8");
		if (tableName == null || "".equals(tableName) || tableName.isEmpty()) {
			return "method不能为空！";
		}
		if ("".equals(sqlType) || sqlType == null || sqlType.isEmpty()) {
			return "sqlType不能为空！";
		}
		tableName = tableName.trim();
		Handle h = new Handle();
		try {
			// 查询
			if ("select".equals(sqlType.trim().toLowerCase())) {
				// 拼接sql语句
				sql = h.getData(request, tableName);
				// 查询数据
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				list = nySereviceImpl.selectData(sql, DBNAME, DbConfig.pg_userName, DbConfig.pg_pwd);
				// String json = JsonUtil.object2Json(list);
				String json = JSONObject.toJSONString(list);
				return json;
			}
			// 删除
			if ("delete".equals(sqlType.trim().toLowerCase())) {
				sql = h.delData(request, tableName);
				if (sql.length() > 0) {
					result = nySereviceImpl.delData(sql, DBNAME, DbConfig.pg_userName, DbConfig.pg_pwd);
					return result;
				}
			}
			// 更新
			if ("update".equals(sqlType.trim().toLowerCase())) {
				sql = h.updateData(request, tableName);
				if (sql.length() > 0) {
					result = nySereviceImpl.updateData(sql, DBNAME, DbConfig.pg_userName, DbConfig.pg_pwd);
					return result;
				}
			}
			// TODO 插入
			if ("insert".equals(sqlType.trim().toLowerCase())) {
				sql = h.insertData(request, tableName);
				if (sql.length() > 0) {
					result = nySereviceImpl.insertData(sql, DBNAME, DbConfig.pg_userName, DbConfig.pg_pwd);
					return result;
				}
			}
		} catch (Exception e) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			e.printStackTrace(new PrintStream(bos));
			String exception = bos.toString();
			return "（数据操作异常）" + exception;
		}
		return "请求响应结束,原因未知！";
	}

}
